﻿using AutoMapper;
using MediatR;
using MyMES.Domain.zyy;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Writer.API.Application.Commends.Works;

namespace MyMES.Writer.API.Application.CommendHandlers.Works
{
    public class UpdateWorksHandler : IRequestHandler<UpdateWorksCommand, APIResult<WorkOrder>>
    {
        private readonly IMapper mapper;
        private readonly IBaseRepository<WorkOrder> baseRepository;
        /// <summary>
        /// 更新生产工单
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="baseRepository"></param>
        public UpdateWorksHandler(IMapper mapper,IBaseRepository<WorkOrder> baseRepository)
        {
            this.mapper = mapper;
            this.baseRepository = baseRepository;
        }

        public async Task<APIResult<WorkOrder>> Handle(UpdateWorksCommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIResult<WorkOrder> result = new APIResult<WorkOrder>();
                result.Code = APIEnum.修改成功;
                result.Msg = "修改成功";
                var model = mapper.Map<WorkOrder>(request);
                string sql = "update WorkOrder set WorkOrderCoding=@WorkOrderCoding,WorkOrderName=@WorkOrderName,WorkOrderType=@WorkOrderType,WorkorderSource=@WorkorderSource,OrderNumber=@OrderNumber,ProductNumber=@ProductNumber,ProductName=@ProductName,Specifications=@Specifications,Unit=@Unit,WorkOrderNum=@WorkOrderNum,AdjustQuantity=@AdjustQuantity,QuantityProduced=@QuantityProduced,LotNumber=@LotNumber,CustomerCode=@CustomerCode,CustomerNamwe=@CustomerNamwe,RequestDate=@RequestDate,State=@State,SupplierCode=@SupplierCode,SupplierName=@SupplierName,Note=@Note,IsDlete=@IsDlete where WorkOrderId=@WorkOrderId";
                await baseRepository.Update(model, sql);
                return result;
            }
            catch (Exception)
            {

                throw;
            }
        
        }
    }
}
